import React, { Component } from 'react';
import {Redirect} from 'react-router-dom'
import Model from "../model/common"
function CheckLogin(Cmp) {
    return class Hoc extends Component {
        state = {
            isLogin:false,
            isFinish:false
        }
        render() {
            return <>
                {this.state.isFinish?this.state.isLogin?<Cmp {...this.props}/>:<Redirect to="/login"/>:<div/>}
            </>
        }



        //在生命周期中判断是否登录
        componentDidMount() {
            let jwt = localStorage.getItem("jwt")

            if(jwt) {
                console.log('有jwt');
                //二次检验jwt是否正确
        Model.jwtPreCheck().then((ret)=>{
            console.log(ret);
            if(ret.errNo===0){
            this.setState(()=>{
                    return {
                        isLogin:true,
                        isFinish:true
                    }
                })
            }
            else
            {
                this.setState(()=>{
                    return {
                        isLogin:false,
                        isFinish:true
                    }
                })
            }
        })
                
            }
            else {
                console.log('没jwt');
                this.setState(()=>{
                    return {
                        isFinish:true
                    }
                })
            }
        }
    }
}
export default CheckLogin;
